#include <avr/io.h>
#include <stdio.h>
#include "util.h"
#include "pll.h"
#include "main.h"

void test_uart(void) {
  char c;
  while (1) {
    for (c='a'; c <= 'z'; c++) {
      uart_putchar(c);
      delay_ms(10);
    }
    LEDPORT |= _BV(LED);
    delay_ms(500);
    LEDPORT &= ~_BV(LED);
    delay_ms(500);
  }
}

void test_led(void) {
  while (1) {
    LEDPORT |= _BV(LED);
    delay_ms(500);
    LEDPORT &= ~_BV(LED);
    delay_ms(500);
  }
}


void test_freq(void) {
  while (1) {
    set_freq_high();
    LEDPORT |= _BV(LED);	
    delay_ms(500);
    set_freq_low();
    LEDPORT &= ~_BV(LED);
    delay_ms(500);
  }
}

void test_resistors(void) {
  uint8_t i = 0;
  while (1) {
    set_resistor(BANDWADJ1_RES, i);
    set_resistor(BANDWADJ2_RES, 255-i);
    delay_ms(50);
    i++;
  }
}

void test_DC(void) {
  uint8_t i = 0;
  LEDPORT |= _BV(LED);	
  while (1) {
    OCR1A = i;
    OCR1B = 255-i;
    i++;
    delay_ms(50);
  }
}

void test_powerswitch(void) {
  while (1) {
    POWERCTL1_PORT &= ~_BV(POWERCTL1);
    POWERCTL2_PORT |= _BV(POWERCTL2);
    delay_ms(2000);
    
    POWERCTL2_PORT &= ~_BV(POWERCTL2);
    POWERCTL1_PORT |= _BV(POWERCTL1);
    delay_ms(2000);
  }
}

void test_vcos(void) { 
  uint8_t i;
  POWERCTL2_PORT |= _BV(POWERCTL2);
  POWERCTL1_PORT |= _BV(POWERCTL1);
  
  
  while (1) {
    set_resistor(BANDWADJ1_RES, 255);
    set_resistor(BANDWADJ2_RES, 255);
    for (i = 0; i< 255; i++) {
      //		set_resistor(BANDWADJ1_RES, i);
      //		set_resistor(BANDWADJ2_RES, i);
      OCR1A = i; 
      OCR1B = i;
      delay_ms(50);
      if(i==0 || i==255) delay_ms(2000);
    }
    
    /*		OCR1A = OCR1B = 127;
		for (i = 0; i< 254; i++) {
		set_resistor(BANDWADJ1_RES, i);
		set_resistor(BANDWADJ2_RES, i);
		delay_ms(50);
		} */
  }
  
}

/*void test_osccal(void) {
	uint8_t i,j;
	for(i=0xa0;i<255;i++) {
		OSCCAL=i;
		delay_ms(100);
		for(j=0;j<10;j++) {
			putstring("Testing OSCAL=");
			putnum_uh(i);
			putstring_nl("");
		}
	}
}
*/
// oscillates pin #10 at 5 Hz
void test_pll1(void) {
  uint32_t out;
  
  out = 0; out <<= 19; out |= (10&0x7FFF);
  pll_tx(out, 0x0); // no otherbits set: defaults
  
  while (1) {
    out = 2; out <<= 19; out |= (10&0x7FFF);
    pll_tx(out, 0x3); // no otherbits set: defaults
    delay_ms(100);
    out = 1; out <<= 19; out |= (10&0x7FFF);
    pll_tx(out, 0x3); // no otherbits set: defaults
    delay_ms(100);
  }
}

uint16_t find_freq_for_dc(uint8_t which, uint16_t val) {
  uint16_t freq, f_lo, f_hi;
  printf_P(PSTR("find_freq_for_dc(%d, %d)\n"), which, val);
  pll_init();
  set_freq_low();
  
  set_resistor(which, 20);
  
  set_dcoff(which, val);
  turn_on_vco(which);
  
  if(which==VCO_HI) {
    f_lo=1000;
    f_hi=3000;
  } else {
    f_lo=500;
		f_hi=2000;
  }
  
  for(freq=f_lo; freq<f_hi; freq+=5) {
    pll_set(which, freq, 8);
    delay_ms(100);
    printf_P(PSTR("\tfreq=%d, PLL=%d (%d)\n"), freq, read_lock_pin_digital(which),
	     read_lock_pin_analog(which));
  } 
  
  freq=f_lo/2 + f_hi/2;  // pick midpoint
  while((f_hi-f_lo)>2) {
    //	  printf_P(PSTR("\tlo = %d, hi = %d\n\r"),f_lo, f_hi);
    pll_set(which, freq, 8);
    delay_ms(10);
    //	  printf_P(PSTR("\tfreq=%d, PLL=%d (%d)\n\r"), freq, read_lock_pin_digital(which),
    //		 	 read_lock_pin_analog(which));
    if(read_lock_pin_digital(which)) f_lo=freq;
    else f_hi=freq;
    freq=f_lo/2 + f_hi/2;  // pick midpoint
  }
  
  return freq;
}

uint16_t  test_bw(uint8_t which, uint16_t center) {
  uint16_t freq, f_lo, f_hi, res, threshold, avg;
  pll_init();
  set_freq_low();
  tune_vco(which, center);
  set_resistor(which, 0);
  pll_set(which, center, 8);
  delay_ms(1000);
  threshold=read_lock_pin_analog(which);
  printf_P(PSTR("Threshold=%d\n"), threshold);
  delay_ms(1000);
  threshold=read_lock_pin_analog(which);
  printf_P(PSTR("Threshold=%d\n"), threshold);
  f_lo=center;
  f_hi=center;
  for(res=0; res<250; res+=5) {
    printf_P(PSTR("Testing res=%d\n"), res);
    set_resistor(which, res);
    for(f_hi=center; f_hi < 3000; f_hi++) {
      pll_set(which, f_hi, 8);
      delay_ms(10);
      avg=read_lock_pin_analog(which);
      //      printf_P(PSTR("f_hi=%d pll=%d\n\r"), f_hi, avg);
      if(avg < 40) break;
    }
    for(f_lo=center; f_lo > 500; f_lo--) {
      pll_set(which, f_lo, 8);
      delay_ms(10);
      avg=read_lock_pin_analog(which);
      //      printf_P(PSTR("f_lo=%d pll=%d\n\r"), f_lo, avg);
      if(avg > 600) break;
    }
    printf_P(PSTR("*** center=%d res=%d lo=%d hi=%d bw=%d\n"),
	     center, res, f_lo, f_hi, f_hi-f_lo);
  }
}


void test_pll2(uint8_t which) {
	uint16_t i;
#if 0
	uint16_t old=0,new;
	printf_P(PSTR("test_pll2(%d, %d)\n\r"), which, freq);

	pll_init();
	
	set_freq_low();
	set_resistor(which, 0);
	set_dcoff(which,3);
	pll_set(which, freq, 8);
	turn_on_vco(which);

	while (1) {
		printf_P(PSTR("OCR1%c=%d, PLL=%d ("), 
			(which == VCO_HI)?'A':'B', get_dcoff(which), read_lock_pin_digital(which));
		new = read_lock_pin_analog(which);
	if(get_dcoff(which)) old = new;
	printf_P(PSTR("%d) delta = %d\n\r"), new, abs(old-new));

	if(abs(old-new)<20) set_dcoff(which, get_dcoff(which)+1); 
	old=new;
//	delay_ms(delay);
	if(get_dcoff(which)==255) {
		freq+=10;
		pll_set(which, freq, 8);
		set_dcoff(which, 3);
	}
	}
#endif
	for(i=5;i<255;i++)
	  printf_P(PSTR("find_freq_for_dc(%d, %d)=%u\n"), which, i, find_freq_for_dc(which, i));
}


void test_pll2_sweep() {
  pll_init();
  
  set_freq_low();
  set_resistor(BANDWADJ2_RES, 0);
  set_resistor(BANDWADJ1_RES, 0);
  
  pll_set(VCO_LOW, 1000, 8);
  pll_set(VCO_HI, 2000, 8);
  turn_on_vco(VCO_LOW);
  turn_on_vco(VCO_HI);
  
  while (1) {
    uint16_t i;
    for(i=3; i<254;i++) {
      set_dcoff(VCO_LOW, i);
      set_dcoff(VCO_HI, i);
      printf_P(PSTR("i=%d, PLL1=%d, PLL2=%d\n"), i,
	       read_lock_pin_analog(VCO_LOW),
	       read_lock_pin_analog(VCO_HI));
      delay_ms(10);
    }
  }
}


void map_if(uint16_t freqstep, uint16_t dcstep) {
  uint16_t freq, dcval;
  pll_init();
  
  set_freq_low();
  set_resistor(BANDWADJ2_RES, 0);
  
  pll_set_if(700, 8);
  POWERCTL2_PORT |= _BV(POWERCTL2); // turn on vco
  
  for(freq=700; freq < 1800; freq+=freqstep) {
    pll_set_if(freq, 8);
    for(dcval=3; dcval<253; dcval+=dcstep) {
      printf_P(PSTR("%d, %d, "), freq, dcval);
      set_dcoff(VCO_LOW, dcval);
      //			delay_ms(delay);
      printf_P(PSTR("%d\n"), read_lock_pin_digital(VCO_LOW));			
    }
  }
}

void step(void) {
  printf_P(PSTR("Step"));
  uart_getchar();
  printf_P(PSTR("\n"));
}
